package com.crazyice.lee.jobs;

import com.alibaba.fastjson.JSONObject;
import com.crazyice.lee.data.Order;
import com.crazyice.lee.data.Product;
import com.crazyice.lee.reader.OrderList;
import com.crazyice.lee.reader.ProductList;
import lombok.extern.slf4j.Slf4j;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.TableEnvironment;
import org.apache.flink.table.api.java.StreamTableEnvironment;
import org.apache.flink.types.Row;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

@Slf4j
public class JoinTableSQLJob {
    private static Properties properties;
    private static JSONObject dataSql;

    private static void init() {
        properties = new Properties();
        try(InputStream inputStream = JoinTableSQLJob.class.getResourceAsStream("/order.properties")) {
            properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //从配置中心获取配置属性
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet httpGet = new HttpGet(properties.getProperty("configUrlString"));
        try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                dataSql = JSONObject.parseObject(EntityUtils.toString(responseEntity));
            }
        } catch (Exception e) {
            log.error("{}", e.getLocalizedMessage());
        }
    }

    public static void main(String[] args) throws Exception{
        init();

        //环境设置
        StreamExecutionEnvironment environment = StreamExecutionEnvironment.getExecutionEnvironment();
        StreamTableEnvironment tableEnv = TableEnvironment.getTableEnvironment(environment);

        //数据源
        DataStreamSource<Product> source1 = environment.addSource(new ProductList());
        DataStreamSource<Order> source2 = environment.addSource(new OrderList());
        source1.name("产品描述");
        source2.name("订单");

        //处理数据
        //1.将StreamData注册为Table
        tableEnv.registerDataStream("product",source1);
        tableEnv.registerDataStream("orderList",source2);
        //2.通过SQL处理数据
        Table orderDetail=tableEnv.sqlQuery(dataSql.getString("orderFeeSQL"));
        Table productSum=tableEnv.sqlQuery(dataSql.getString("productSumSQL"));
        //3.将Table转换为DataStream
        DataStream<Row> resultOrder=tableEnv.toAppendStream(orderDetail,Row.class);
        DataStream<Tuple2<Boolean,Row>> resultProduct=tableEnv.toRetractStream(productSum,Row.class).filter(e->e.f0);

        //结果输出
        resultOrder.print().name("输出订单到out文件");
        resultProduct.print().name("输出卖出产品数量到out文件");
        environment.execute("多表联查");
    }
}
